package LintCode;

import java.util.*;

/**
 * @Classname AttenMeetings
 * @Description TODO
 * @Date 2021/3/6 11:54
 * @Created by ASUS
 */
public class AttenMeetings {

    /**
     * 给定一系列的会议时间间隔，包括起始和结束时间[[s1,e1]，[s2,e2]，…(si < ei)，
     * 确定一个人是否可以参加所有会议。
     * 样例
     * <p>
     * 样例1
     * <p>
     * 输入: intervals = [(0,30),(5,10),(15,20)]
     * 输出: false
     * 解释:
     * (0,30), (5,10) 和 (0,30),(15,20) 这两对会议会冲突
     * <p>
     * 样例2
     * <p>
     * 输入: intervals = [(5,8),(9,15)]
     * 输出: true
     * 解释:
     * 这两个时间段不会冲突
     */

    static class Interval {
        int start, end;

        Interval(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    /**
     * @param intervals: an array of meeting time intervals
     * @return: if a person could attend all meetings
     */
    public boolean canAttendMeetings(List<Interval> intervals) {
        // Write your code here
        if (intervals == null || intervals.size() < 2) return true;

        // 当时间的起始时间是按照升序排序好的时候，下列方法可以解决
        // s1 < s2 < s3 < ... < si
        // for (int i = 0; i < intervals.size() - 1; i++) {
        //     Interval in1 = intervals.get(i);
        //     Interval in2 = intervals.get(i+1);
        //     if (in1.end >= in2.start) return false;
        // }
        // return true;

        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> startList = new ArrayList<>();
        List<Integer> endList = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        for (Interval inter : intervals) {
            int start = inter.start;
            int end = inter.end;
            if (map.containsKey(start) || set.contains(end)) return false;
            map.put(start, end);
            set.add(end);
            startList.add(start);
            endList.add(end);
        }

        Collections.sort(startList);
        Collections.sort(endList);
        System.out.println(startList);
        System.out.println(endList);
        for (int i = 0; i < startList.size(); i++) {
            System.out.println(map.get(startList.get(i)) + "\t,\t" + endList.get(i));
            if (!map.get(startList.get(i)).equals(endList.get(i))) return false;
        }
        return true;
    }

    private static List<Interval> sortIntervals(List<Interval> intervals) {
        List<Integer> startList = new ArrayList<>();
        List<Integer> endList = new ArrayList<>();
        for (Interval interval : intervals) {
            startList.add(interval.start);
            endList.add(interval.end);
        }
        Collections.sort(startList);
        Collections.sort(endList);
        System.out.println(startList);
        System.out.println(endList);
        List<Interval> ans = new ArrayList<>();
        for (int i = 0; i < startList.size(); i++) {
            ans.add(new Interval(startList.get(i), endList.get(i)));
        }
        return ans;
    }

    private static List<Interval> buildIntervals(int[][] intervals) {
        List<Interval> ans = new ArrayList<>(intervals.length);
        for (int[] interval : intervals) {
            ans.add(new Interval(interval[0], interval[1]));
        }
        return ans;
    }

    private static List<Interval> buildIntervals(int[][] intervals, int[][] ask) {
        if (ask == null || ask.length == 0) {
            return buildIntervals(intervals);
        }
        List<Interval> intervals1 = buildIntervals(intervals);
        List<Interval> intervals2 = buildIntervals(ask);
        intervals1.addAll(intervals2);
        return intervals1;
    }

    /**
     * @param intervals: the intervals
     * @param rooms:     the sum of rooms
     * @param ask:       the ask
     * @return: true or false of each meeting
     * 输入:Intervals:[[1,2],[4,5],[8,10]],rooms = 1,ask: [[2,3],[3,4]]
     * 输出: [true,true]
     * 解释:
     * 对于[2,3]的询问，我们可以安排一个会议室room0。
     * 以下是room0的会议列表：
     * [[1,2],[2,3],[4,5],[8,10]]
     * 对于[3,4]的询问，我们可以安排一个会议室room0。
     * 以下是room0的会议列表：
     * [[1,2],[3,4],[4,5],[8,10]]
     *
     * don't solved.
     */
    public boolean[] meetingRoomIII(int[][] intervals, int rooms, int[][] ask) {
        // Write your code here.
//        List<Interval> intervalsList = sortIntervals(buildIntervals(intervals, ask));

        int[] start = new int[intervals.length];
        int[] end = new int[intervals.length];

        for (int i = 0; i < start.length; i++) {
            start[i] = intervals[i][0];
            end[i] = intervals[i][1];
        }

        Arrays.sort(start);
        Arrays.sort(end);



        boolean[] res = new boolean[ask.length];
        int index = 0;


        Map<int[], Integer> map = new HashMap<>();
        for (int i = 0; i < ask.length; i++) {
            map.put(ask[i], i);
        }


        return null;
    }

    public static void main(String[] args) {
        List<Interval> intervals = new ArrayList<>();
        intervals.add(new Interval(465, 497));
        intervals.add(new Interval(386, 462));
        intervals.add(new Interval(354, 380));
        intervals.add(new Interval(134, 189));
        intervals.add(new Interval(199, 282));
        intervals.add(new Interval(18, 104));
        intervals.add(new Interval(499, 562));
        intervals.add(new Interval(4, 14));
        intervals.add(new Interval(114, 129));
        intervals.add(new Interval(292, 345));
        int[][] meetings = new int[][]{
                {1, 2},
                {4, 5},
                {8, 10}
        };
        int[][] ask = new int[][]{
                {2, 3},
                {3, 4}
        };
        System.out.println(Arrays.toString(new AttenMeetings().meetingRoomIII(meetings, 1, ask)));
    }
}
